{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "69907877-8710-4ec1-94dc-2b5db99c9810",
   "metadata": {},
   "outputs": [],
   "source": [
    "import tensorflow as tf\n",
    "s=[[1],[2,3],[4,5,6]]\n",
    "print(\"填充前序列为\\n\",s)\n",
    "a=tf.keras.preprocessing.sequence.pad_sequences(s,padding='post')\n",
    "print(\"填充后序列为\\n\",a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "54016502-ce06-4f9a-919d-1febc6c7ffb0",
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install tensorflow"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fa2811aa-9d5a-47ea-8a82-bc150dc82c98",
   "metadata": {},
   "outputs": [],
   "source": [
    "import tensorflow as tf\n",
    "s=[[1,2,3,4,9],[4,5,6],[7,8]]\n",
    "print(\"剪裁前序列为\\n\",s)\n",
    "a=tf.keras.preprocessing.sequence.pad_sequences(s,maxlen=2)\n",
    "print(\"剪裁后序列为\\n\",a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8bbaf9b5-8ebe-40e8-aa57-2e6b88fe75cf",
   "metadata": {},
   "outputs": [],
   "source": [
    "import tensorflow as tf\n",
    "import numpy as np\n",
    "model=tf.keras.Sequential()\n",
    "embedding=tf.keras.layers.Embedding(output_dim=32,input_dim=100,input_length=400)\n",
    "model.add(embedding)\n",
    "model.summary()\n",
    "print(\"embeddings矩阵=\\n\",embedding.get_weights())\n",
    "text=\"Deep learning is an important concept raised by the current sciences.\"\n",
    "token=tf.keras.preprocessing.text.Tokenizer(num_words=100)\n",
    "token.fit_on_texts(text)\n",
    "imput=token.texts_to_sequences(text)\n",
    "test_seq=tf.keras.preprocessing.sequence.pad_sequences(input,padding='post',maxlen=400,truncating='post')\n",
    "output_array=model.predict(test_seq)\n",
    "print(\"输出矩阵的形状=\",output_array.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "19e508ce-70cd-4cd6-a196-aad371d1a571",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
